/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.widget;

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Image;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Location;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.StringUtil;
import org.mobiubiq.directory.client.KuixClient;

/**
 *
 * @author Marnix Renne
 */
public class GoogleMapsWidget extends Widget {
    public static final String GOOGLE_MAPS_WIDGET_TAG = "googlemaps";

    public GoogleMapsWidget(String pictureId, Location location) {
        super(GOOGLE_MAPS_WIDGET_TAG);
        try {
            Coordinates coordinates = location.getQualifiedCoordinates();
            String apiKey = KuixClient.getDefault().getAppProperty("google_api_key");
            Picture mapImage = (Picture) Kuix.getCanvas().getDesktop().getWidget(pictureId);
            int zoom = 7;
            int width = Kuix.getCanvas().getDesktop().getWidth();
            int height = Kuix.getCanvas().getDesktop().getHeight();
            double lat = coordinates.getLatitude();
            double lon = coordinates.getLongitude();
            String url = "http://maps.google.com/staticmap?maptype=mobile&center={0},{1}&markers={0},{1},redc&size={2}x{3}&zoom={4}&key={5}";
            url = StringUtil.format(url, new Object[]{new Double(lat), new Double(lon), new Integer(width), new Integer(height), new Integer(zoom), apiKey});

            Image image = Image.createImage(Connector.openInputStream(url));
            mapImage.setImage(image);
        } catch (IOException ex) {
            Kuix.alert(ex.getMessage());
        }
    }
}
